%{
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

void yyerror(const char *s);
extern int yylex(void);
double vbltable[26];

#ifdef __cplusplus
}
#endif

%}

%union {
	double dval;
	int vblno;
}

%token <vblno> NAME
%token <dval> NUMBER
%left '-' '+'
%left '*' '/'

%type <dval> expression
%%
statement_list:statement_list statement '\n' {printf("yacc list state\n");}
	|statement '\n'{printf("yacc state\n");}

statement: NAME '=' expression {vbltable[$1]=$3;printf("yacc =\n");}
	| expression {printf("=%f\n", $1);}

expression:expression '+' expression {$$ = $1 + $3;}
	| expression '-' expression {$$ = $1 - $3;}
	| expression '*' expression {$$ = $1 * $3;printf("yacc *\n");}
	| expression '/' expression {
		if ($3 == 0) {
			yyerror("divide by zero!!!");
		} else {
			$$ = $1 / $3;
		}
	}
	| NUMBER {printf("yacc number\n");}
	| NAME {$$=vbltable[$1];printf("yacc name\n");}
%%
void yyerror(const char *s)
{
	printf("%s\r\n", s);
}

extern FILE *yyin;
int main()
{
#ifdef YYDEBUG
	yydebug = 1;	
#endif	
	do {
		yyparse();
	}while(!feof(yyin));
	
	return 0;
}
